package com.sgb.goods.view.adapter;

import android.view.View;

import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableInt;
import androidx.databinding.ViewDataBinding;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.entity.GoodsManageListEntity;
import com.sgb.goods.utils.base.adapter.BaseCommonAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品管理列表编辑适配器
 * @author cyj
 */
public class GoodsManageListEditAdapter extends BaseCommonAdapter<GoodsManageListEntity> implements BaseQuickAdapter.OnItemClickListener {

    /**
     * 选中计数器
     */
    public ObservableInt selectNum = new ObservableInt();
    /**
     * 是否选择全部
     */
    public ObservableBoolean isSelectAll = new ObservableBoolean();

    public GoodsManageListEditAdapter() {
        super(R.layout.goods_item_goods_manage_list_edit, BR.viewModel);
        setOnItemClickListener(this);
    }

    @Override
    protected void convert(BaseViewHolder baseHolder, ViewDataBinding dataBinding, GoodsManageListEntity entity) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        GoodsManageListEntity entity = getData().get(position);
        entity.isSelectIt.set(!entity.isSelectIt.get());
        // 计数器根据选中+1反之-1
        selectNum.set(entity.isSelectIt.get() ? selectNum.get() + 1 : selectNum.get() - 1);
    }

    /**
     * 设置全选与非全选
     * @param isSelectAll    是否全选
     */
    public void setSelectAll(boolean isSelectAll) {
        this.isSelectAll.set(isSelectAll);
        for (GoodsManageListEntity entity : getData()) {
            entity.isSelectIt.set(isSelectAll);
        }
        selectNum.set(isSelectAll ? getData().size() : 0);
    }

    /**
     * 获取选中的商品列表
     * @return    List<String>
     */
    public List<String> getSelectList() {
        List<String> list = new ArrayList<>();
        for (GoodsManageListEntity entity : getData()) {
            // 这个item被选中了
            if(entity.isSelectIt.get()) {
                list.add(entity.id);
            }
        }
        return list;
    }
}
